<template>
  <!-- <div>
        <h2>Home页面
        <el-button type="info" @click="logout()">登出</el-button>
  </div>-->

  <!-- 引入container布局 -->

  <!-- 头部 -->
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt />
        <span>DMP数据管理平台</span>
      </div>
      <div class="home_userinfoContainer">
        <el-dropdown>
          <span class="el-dropdown-link home_userinfo">
            {{currentUser}}
            <i class="el-icon-arrow-down el-icon--right home_userinfo"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="myhome">个人主页</el-dropdown-item>
            <el-dropdown-item @click.native="dialogVisible = true">修改密码</el-dropdown-item>
            <!-- <el-button type="text" @click="dialogVisible = true">修改密码</el-button> -->
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle_button" @click="toggleCollapse()">|||</div>
        <el-menu
          background-color="#EEF1F6"
          class="el-menu-vertical-demo"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu :index="item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObject[item.id]"></i>
              <span> {{item.title}}</span>
            </template>
            <el-menu-item
              :index="it.path"
              v-for="it in item.subList"
              :key="it.id"
              @click="saveNavState(it.path)"
            >
              <template slot="title">
                <i :class="iconsObject[it.id]"></i>
                <span> {{it.title}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main>
        <router-view></router-view>
        <el-dialog
          title="修改密码"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <!-- 内容主体区域 -->
          <el-form :model="updateForm" ref="updateFormRef" label-width="70px">
            <!-- 原密码 -->
            <el-form-item label="原密码" prop="username">
              <el-input v-model="updateForm.password" show-password></el-input>
            </el-form-item>
            <!-- 新密码 -->
            <el-form-item label="新密码" prop="newpassword">
              <el-input v-model="updateForm.newpassword" show-password></el-input>
            </el-form-item>
            <!-- 确认密码 -->
            <el-form-item label="确认密码" prop="newpassword1">
              <el-input v-model="updateForm.newpassword1" show-password></el-input>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="updatePassword()">确定</el-button>
          </span>
        </el-dialog>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false,
      dialogVisible: false,
      iconsObject: {
        100: "iconfont iconshezhi1",
        200: "iconfont iconshuju1",
        300: "iconfont iconxieyuanguanxi2",
        400: "iconfont iconyingsheguanxi",
        101: "iconfont iconyonghu",
        102: "iconfont iconjiaose",
        103: "iconfont iconquanxian",
        104: "iconfont iconmenuManager",
        201: "iconfont iconbiaodanliebiao-",
        202: "iconfont iconrenwu",
        203: "iconfont iconjiankong",
        204: "iconfont iconyujing",
        301: "iconfont iconxieyuanfenxi",
        302: "iconfont iconxieyuan",
        303: "iconfont iconzhanshixieyuanlianlu",
        304: "iconfont iconshujuwajue",
        401: "iconfont iconxiugai1",
        402: "iconfont iconicon",
      },
      defaultPath: "/welcome", //默认访问路径
      updateForm: {
        password: "",
        newpassword: "",
        newpassword1: "",
      },
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
    this.currentUser = window.sessionStorage.getItem("username");
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    logout() {
      window.sessionStorage.clear(); //清除session
      this.$router.push("/login");
    },
    myhome() {
      this.$router.push("/welcome");
    },
    async getMenuList() {
      // console.log("getMenuList");
      const { data: res } = await this.$http.get("/menu");
      console.log(res);
      if (res.flag != 200) return this.$message.error("获取列表失败"); //获取菜单列表失败
      this.menuList = res.menu; //获取菜单列表成功
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存菜单路径
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
    async updatePassword() {
      if (this.updateForm.newpassword != this.updateForm.newpassword1) {
        this.$message.error("新密码与确认密码输入不一致");
      } else {
        const { data: res } = await this.$http.post(
          "updatepassword?id=" +
            window.sessionStorage.getItem("userid") +
            "&password=" +
            this.updateForm.password +
            "&newpassword=" +
            this.updateForm.newpassword
        );
        if (res != "Success") {
          return this.$message.error("修改失败");
        }
        this.$message.success("修改成功");
        this.$refs.updateFormRef.resetFields();
        this.dialogVisible = false;
      }
    },
  },
};
</script>
<style lang="less">
.home-container {
  height: 100%;
}
img {
  height: 40px;
  width: 40px;
}

.el-header {
  background-color: #324157;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  color: #fff;
  align-items: center;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #eef1f6;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #fff;
}
.toggle_button {
  font-size: 10px;
  line-height: 24px;
  color: black;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer; //显示小手
}
.home_userinfoContainer {
  display: inline;
  margin-right: 20px;
}
.home_userinfo {
  color: #fff;
  cursor: pointer;
}
body .el-table th.gutter{
display: table-cell!important;
}
</style>